package service

import (
	"cgs-server/server/cgs/app/models"
	v1 "cgs-server/server/cgs/app/models/v1"
	"cgs-server/server/cgs/app/store"
	"cgs-server/server/model"
	"go.mongodb.org/mongo-driver/bson/primitive"
)

type WorksheetViewSrv interface {
	Save(wg *models.WorksheetView, user *model.User) error
	Delete(viewId string, user *model.User) error
	Update(wg *models.WorksheetView, user *model.User) error
}

type worksheetViewSrv struct {
	store store.Factory
}

func (w *worksheetViewSrv) Update(wg *models.WorksheetView, user *model.User) error {
	err := w.store.WorksheetView().Update(wg, user, v1.UpdateOptions{})
	return err
}

func (w *worksheetViewSrv) Delete(viewId string, user *model.User) error {
	viewObjId, err := primitive.ObjectIDFromHex(viewId)
	if err != nil {
		return err
	}
	err = w.store.WorksheetView().Delete(&models.WorksheetView{Id: viewObjId}, user, v1.DeleteOptions{})
	return err
}

func (w *worksheetViewSrv) Save(wg *models.WorksheetView, user *model.User) error {
	err := w.store.WorksheetView().Save(wg, user, v1.CreateOptions{})
	return err
}

func newWorksheetViewSrv(s *service) WorksheetViewSrv {
	return &worksheetViewSrv{
		store: s.store,
	}
}
